package swd.basic.conf_prg_button;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.pub.database.QueryBean;
import com.pub.database.DataView;
import com.pub.database.DataViewForm;
import com.pub.web.WebFunction;;

public class Action {
	QueryBean gqbnDAO = null;
    public String action(javax.servlet.jsp.PageContext pageContext) {
        HttpServletRequest request = (HttpServletRequest) pageContext.
                                     getRequest();
        HttpServletResponse response = (HttpServletResponse) pageContext.
                                       getResponse();

        View view = (View) request.getAttribute("view");
        (new WebFunction()).setPortal_to_session(request); //set session from portal
        if (request.getSession().getAttribute("u_id") == null) {
            try {
                request.getRequestDispatcher((new StringBuffer("login.jsp")).
                                             insert(0, "/").toString()).forward(
                        request, response);
                return "login";
            } catch (Exception le) {
                view.setOnloadScript(
                        "can not redirect to login page...please contact IT");
                return "";
            }
        }
        String lstrUserNO = request.getSession().getAttribute("u_id").toString();
        gqbnDAO = new QueryBean(view.getDBAlias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());

        DataView DataView = new DataView(gqbnDAO);
        DataViewForm DataViewForm = new DataViewForm();

        request.setAttribute("DataViewForm", DataViewForm);

        Business business = new Business(gqbnDAO);

        view.setRequest(request);
        view.setResponse(response);
        view.setDataView(DataView);
        view.setDataViewForm(DataViewForm);

        String lstrResult = null;

        try {
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("getData")) { //利用AJAX即時讀取資料
                lstrResult = business.getData(view, lstrUserNO);

                pageContext.getOut().clear();
                pageContext.getOut().write(lstrResult);
                pageContext.getOut().flush();
                return "login";
            }

            business.initAction(view);

            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("batchUpdate")) {
                lstrResult = business.batchUpdateAction(view, lstrUserNO,
                        request);
            }

            business.endAction(view);

            return "";
        } catch (Exception e) {
            if (view.getDBDebug()) {
                e.printStackTrace();
            }
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("getData")) { //利用AJAX即時讀取資料
                return "login";
            } else {
                view.setOnloadScript(
                        "alert('Program error please contact IT.');");
            }
            return "";
        } finally {
            try {
                if (gqbnDAO != null) {
                    gqbnDAO.close();
                }
            } catch (Exception e) {
                System.out.println("can not close querybean in Action.java");
            }
        }
    }
}
